<template>
	<view>
		<cu-custom bgColor="bg-white" :isBack="true">
			<block slot="backText" @tap="back()">返回</block>
			<block slot="content">编辑信息</block>
		</cu-custom>
		<view class="cu-form-group margin-top">
			<view class="title">头像</view>
			<image class="cu-avatar xl radius bg-gray" style="margin: 30rpx;" :src="userInfo.avatarUrl"></image>
			<!-- <view class="cu-avatar xl radius bg-gray" :style="'background-image:'"+url(userInfo.avatarUrl+)"></view> -->
		</view>
		<view class="cu-form-group margin-top">
			<view class="title">昵称</view>
			<input placeholder="" v-model="userInfo.nickName" name="input" disabled></input>
		</view>
		<!-- <view class="cu-form-group">
			<view class="title">账号</view>
			<input placeholder="" v-model="userInfo.userAccount" name="input"></input>
		</view> -->
		<view class="cu-form-group">
			<view class="title">性别</view>
			<switch class="switch-sex" :class="userInfo.gender==1?'checked':''" :checked="userInfo.gender==1" @change="sexChange"></switch>
		</view>
		<view class="cu-form-group margin-top">
			<view class="title">邮箱</view>
			<input placeholder="未设置" v-model="userInfo.userEmail" name="input"></input>
			<text class='cuIcon-mail text-orange'></text>
		</view>
		<view class="cu-form-group">
			<view class="title">电话</view>
			<input placeholder="未设置" v-model="userInfo.userPhone" name="input"></input>
			<view class="cu-capsule radius">
				<view class='cu-tag bg-blue '>
					+86
				</view>
				<view class="cu-tag line-blue">
					中国大陆
				</view>
			</view>
		</view>
		<view class="cu-form-group">
			<view class="title">加入日期</view>
			<picker mode="date" v-model="userInfo.createTime" start="2021-01-01" end="2022-01-01" @change="DateChange">
				<view class="picker">
					<text v-if="userInfo.createTime">
						{{ userInfo.createTime.split('T')[0] }}
					</text>
				</view>
			</picker>
		</view>
		<!-- <view class="margin-top padding-lr-xl flex flex-direction">
			<button class="btn-wx" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">获取手机号</button>
		</view> -->
		<view class="margin-top padding-lr-xl flex flex-direction">
			<button class="cu-btn bg-blue margin-tb-sm lg shadow" @tap="saveInfo">保存信息</button>
		</view>
	</view>
</template>

<script>
	import WXBizDataCrypt from '@/common/WXBizDataCrypt.js'
	export default {
		data() {
			return {
				userInfo: {},
				appid: 'wx8ff9ea4785bb53d3',
				secret: 'e19f50a095e20be05f77580217b48f1f',
			};
		},
		onLoad() {
		},
		onShow() {
			this.userInfo = uni.getStorageSync('userInfo')
		},
		methods: {
			DateChange(e) {
				this.userInfo.createTime = e.detail.value
			},
			// 获取wechat手机号
			getPhoneNumber(e) {
				console.log('userPhoneRes: ', e)
				let self = this
				if (e.detail.errMsg === "getPhoneNumber:ok") {
					let c = new WXBizDataCrypt(self.appid, self.userInfo.session_key);
					let d = c.decryptData(e.detail.encryptedData, e.detail.iv);
					console.log('res: ', d)
					self.userInfo.userPhone = d.phoneNumber
					// 修改微信账号手机号
					// ...
				} else {
					uni.showToast({
						title: "已拒绝获取微信手机号",
						icon: "none",
						duration: 2000
					})
				}
			},
			sexChange(e) {
				let ck = e.detail.value
				if (ck) {
					this.userInfo.gender = 1
				} else {
					this.userInfo.gender = 2
				}
			},
			saveInfo() {
				let self = this
				uniCloud.callFunction({
					name:'update_user',
					data: self.userInfo,
					success(r2){
						let id = r2.result.data
						self.userInfo._id = id
						uni.setStorageSync('userInfo', self.userInfo)
						uni.showToast({
							title: "更新成功",
							icon: "success"
						})
					},
					fail(e){ console.log(e) }
				})
			}
		}
	}
</script>

<style lang="scss">

</style>
